package br.mil.eb.pqrmnt7.scc.persistence.jpa.quotacombustivel;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import br.mil.eb.pqrmnt7.scc.entity.FonteCombustivel;
import br.mil.eb.pqrmnt7.scc.entity.QuotaCombustivel;
import br.mil.eb.pqrmnt7.scc.entity.QuotaCombustivelEntity;
import br.mil.eb.pqrmnt7.scc.entity.TipoCombustivel;
import br.mil.eb.pqrmnt7.scc.persistence.jpa.AppJpaDAO;

import com.powerlogic.jcompany.commons.PlcBaseContextVO;
import com.powerlogic.jcompany.commons.PlcException;
import com.powerlogic.jcompany.commons.annotation.PlcAggregationDAOIoC;
import com.powerlogic.jcompany.commons.config.stereotypes.SPlcDataAccessObject;
import com.powerlogic.jcompany.persistence.jpa.PlcQuery;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryFirstLine;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryLineAmount;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryOrderBy;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryParameter;
import com.powerlogic.jcompany.persistence.jpa.PlcQueryService;
/**
 * Classe de Persistência gerada pelo assistente
 */

@PlcAggregationDAOIoC(QuotaCombustivelEntity.class)
@SPlcDataAccessObject
@PlcQueryService
public class QuotaCombustivelDAO extends AppJpaDAO  {

	private static final Log logger = LogFactory.getLog(QuotaCombustivelDAO.class);	
	
	@Inject
	PlcBaseContextVO context;
	
	@PlcQuery("querySel")
	public native List<QuotaCombustivelEntity> findList(
			PlcBaseContextVO context,
			@PlcQueryOrderBy String dynamicOrderByPlc,
			@PlcQueryFirstLine Integer primeiraLinhaPlc, 
			@PlcQueryLineAmount Integer numeroLinhasPlc,		   
			
			@PlcQueryParameter(name="dataFornecimento", expression="obj.dataFornecimento = :dataFornecimento") Date dataFornecimento,
			@PlcQueryParameter(name="tipoCombustivel", expression="obj1 = :tipoCombustivel") TipoCombustivel tipoCombustivel,
			@PlcQueryParameter(name="fonteCombustivel", expression="obj2 = :fonteCombustivel") FonteCombustivel fonteCombustivel
	);

	@PlcQuery("querySel")
	public native Long findCount(
			PlcBaseContextVO context,
			
			@PlcQueryParameter(name="dataFornecimento", expression="obj.dataFornecimento = :dataFornecimento") Date dataFornecimento,
			@PlcQueryParameter(name="tipoCombustivel", expression="obj1 = :tipoCombustivel") TipoCombustivel tipoCombustivel,
			@PlcQueryParameter(name="fonteCombustivel", expression="obj2 = :fonteCombustivel") FonteCombustivel fonteCombustivel
	);

	public List<QuotaCombustivel> recuperaPorPeriodo(Date dataInicio, Date dataFim) {
		try {
			
			logger.debug("Chegou ao recupera no OrdemAbastecimentoDAO");
			
			EntityManager entityManager = getEntityManager(context);
			
			logger.debug("dataInicio: " + dataInicio);
			logger.debug("dataFim: " + dataFim);
			
			List resultList = entityManager.createNamedQuery("QuotaCombustivelEntity.recuperaPorPeriodo")
										   .setParameter("dataInicio",dataInicio)
										   .setParameter("dataFim",dataFim)
									       .getResultList();
			
			logger.debug("Número de entidades recuperadas: " + resultList.size());
			
			return resultList;
			
		} catch (Exception e) {
			logger.error(e);
			throw new PlcException("QuotaCombustivelDAO","recuperaPorPeriodo",e,log,"");
		}
	}
	
}
